package fun.werfamily.wf.order.core.domain.remote.facade;

import com.alibaba.fastjson.JSON;
import fun.werfamily.wf.base.vo.Result;
import fun.werfamily.wf.order.core.domain.dto.req.user.UserAddressIdDTO;
import fun.werfamily.wf.order.core.domain.dto.req.user.UserIdDTO;
import fun.werfamily.wf.order.core.domain.dto.result.user.UserAddressDTO;
import fun.werfamily.wf.order.core.domain.dto.result.user.UserInfoDTO;
import fun.werfamily.wf.order.core.domain.remote.service.UserRemoteService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;


/**
 * Created with IntelliJ IDEA.
 *
 * @Author: Mr.WenMing Zhang
 * @Date: 2022-08-28 17:33
 * @Description:
 */
@Component
@Slf4j
public class UserServiceFacade {
    @Resource
    private UserRemoteService userRemoteService;

    /**
     * 获取用户详情
     *
     * @param userId
     * @return
     */
    public UserInfoDTO userInfo(String userId) {
        UserIdDTO userIdDTO = new UserIdDTO();
        userIdDTO.setUserCode(userId);
        try {
            Result<UserInfoDTO> result = userRemoteService.userInfo(userIdDTO);
            log.info("调用user-core获取用户详情,req={},resp={}", JSON.toJSONString(userIdDTO), JSON.toJSONString(result));
            if (result.isSuccess()) {
                return result.getData();
            }
        } catch (Exception e) {
            log.error("调用user-core获取用户详情，异常req={}", JSON.toJSONString(userIdDTO), e);
        }
        return null;
    }

    /**
     * 获取收货地址详情
     *
     * @param addressId
     * @return
     */
    public UserAddressDTO getAddressById(Long addressId) {
        UserAddressIdDTO userAddressIdDTO = new UserAddressIdDTO();
        userAddressIdDTO.setAddressId(addressId);
        try {
            Result<UserAddressDTO> result = userRemoteService.getAddressById(userAddressIdDTO);
            log.info("调用user-core获取收货地址详情,req={},resp={}", JSON.toJSONString(userAddressIdDTO), JSON.toJSONString(result));
            if (result.isSuccess()) {
                return result.getData();
            }
        } catch (Exception e) {
            log.error("调用user-core获取收货地址详情，异常req={}", JSON.toJSONString(userAddressIdDTO), e);
        }
        return null;
    }

    /**
     * 获取默认收获地址
     *
     * @param userCode
     * @return
     */
    public UserAddressDTO getDefaultAddr(String userCode) {
        UserIdDTO userIdDTO = new UserIdDTO();
        userIdDTO.setUserCode(userCode);
        try {
            Result<UserAddressDTO> result = userRemoteService.getDefaultAddr(userIdDTO);
            log.info("调用user-core获取默认收获地址,req={},resp={}", JSON.toJSONString(userIdDTO), JSON.toJSONString(result));
            if (result.isSuccess()) {
                return result.getData();
            }
        } catch (Exception e) {
            log.error("调用user-core获取默认收获地址，异常req={}", JSON.toJSONString(userIdDTO), e);
        }
        return null;
    }
}
